#!/bin/bash
#
# Gerador de senhas
# Autor: Ricardo Brito do Nascimento
# email: rbrito1978-blessed@yahoo.com.br
#
VERSION=1
STRING[0]='01'
STRING[1]='01234567'
STRING[2]='0123456789'
STRING[3]='0123456789abcdef'
STRING[4]='0123456789abcdefghijklmnopqrstuvxywzABCDEFGHIJKLMNOPQRSTUVXYWZ'
STRING[5]='0123456789abcdefghijklmnopqrstuvxywzABCDEFGHIJKLMNOPQRSTUVXYWZ!@#$%&*()_+-=:./?~'
DEFAULT=${STRING[5]}
LENGTH=''
TYPE=''

_help() {
echo "
  $0 [length type | -h |--help|-v]
  DEFAULT: $0

  This script generate passwords based binary, hexadecimal,
  decimal, octal, alphanumeric, symbols and your string.

 Options: 
  -h -> this help;
  --help -> this help;  
  -v -> version;

  exemple: ./$0	- generate symbols passwords with length 8;
	   ./$0 15 alf - generate alphanumeric passwords with length 15;
	   sh $0  8 sym - generate symbols passwords with length 8;

  where type are:
    hexadecimal or hex
    decimal     or dec
    octal       or oct
    binary     or bin
    alphanumeric or alf
    symbols     or sym
    string enter

"
  exit 0
}
_version(){
echo version $VERSION
}

if [ -z $1 ]; then
  LENGTH=8
  TYPE='sym' 
elif [ $# -eq 2 ]; then
  LENGTH=$1
  TYPE=$2
elif [ $# -eq 1 ]; then
  LENGTH=0
  TYPE=$1
else
  LENGTH=0
  TYPE=''
fi

case $TYPE in
  'binarie'|'bin')
    	STR=${STRING[0]};
  ;;
  'octal'|'oct')
    	STR=${STRING[1]};
  ;;
  'decimal'|'dec')
    	STR=${STRING[2]};
  ;;
  'hexadecimal'|'hex')
    	STR=${STRING[3]};
  ;;
  'alfanumeric'|'alf')
    	STR=${STRING[4]};
  ;;
  'symbols'|'sym')
	STR=${STRING[5]};
  ;;
  '-h'|'--help')
	_help;
  ;;
  '-v')
	_version;
  ;;
  *)
  	echo "$0 length type
	 for more details use:
	 $0 --help
	" 
  ;;
esac

BASE=`echo -n $STR | wc -c`

for ((i=1; i<=$LENGTH;i++)); 
do 
  N=$(( $RANDOM % $BASE + 1 ))
  echo $STR | cut -c$N
done | tr -d '\012'
echo
